package com.colin.aiyi.ui.Home.teacherSay;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.colin.aiyi.R;
import com.colin.aiyi.adapter.OneSayAdapter;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.entity.BannerEntity;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.entity.response.YiJuAndYiShuoResponse;
import com.colin.aiyi.eventbus.EventFloatButton;
import com.colin.aiyi.fragment.BaseFragment;
import com.colin.aiyi.utils.MusicUtil;
import com.colin.aiyi.utils.TimeUtil;
import com.colin.aiyi.view.CustomLoadMoreView;
import com.colin.aiyi.view.ShowMsgUtil;
import com.flyco.tablayout.SegmentTabLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGABanner;
import de.hdodenhof.circleimageview.CircleImageView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by hl on 2017/5/5.
 * 导师说，
 */

public class WordFragment2WithNewRefresh extends BaseFragment implements IViewSayTeacher {


    @BindView(R.id.banner_guide_content)
    BGABanner bannerGuideContent;
    @BindView(R.id.img)
    CircleImageView img;
    @BindView(R.id.text_mooth)
    TextView textMooth;
    @BindView(R.id.text_date)
    TextView textDate;
    @BindView(R.id.text_decs)
    TextView textDecs;
    @BindView(R.id.text_pulish)
    TextView textPulish;
    @BindView(R.id.text_no_yulu)
    TextView textNoYulu;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.tablayout)
    SegmentTabLayout tablayout;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.text_search)
    TextView textSearch;
    @BindView(R.id.text_no_data)
    TextView textNoData;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.fragment_rotate_header_with_text_view_frame)
    PtrClassicFrameLayout hFrame;
    private Unbinder unbinder;
    private CustomLoadMoreView customLoadMoreView;
    private OneSayAdapter headerAndFooterAdapter;
    BaseRequest baseRequest = new BaseRequest();
    private List<YiJuAndYiShuoEntity> myWordList = new ArrayList<>();
    BannerEntity bannerEntity;
    ImpPSay iPresenter;

    public static final String[] tabs = {"最新", "人气", "已购"};

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new ImpPSay(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout_say2_with_new_refresh, null);
        unbinder = ButterKnife.bind(this, view);
        initHeadView(view);
        initAdapter();
        iPresenter.activity_list(baseRequest);
        baseRequest.setPageSize("1");
        iPresenter.discourseList(baseRequest);
        //  ShowMsgUtil.showLoadingDialog(getContext());
        return view;
    }

    private void initHeadView(View headView) {

        hFrame.setLastUpdateTimeRelateObject(this);
        hFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                iPresenter.onRefresh();
            }
        });
        tablayout.setOnTabSelectListener(iPresenter);
        tablayout.setTabData(tabs);

//        tablayout.setCurrentTab(0);
        iPresenter.onTabSelect(0);
        textSearch.setOnClickListener(iPresenter);
        bannerGuideContent = (BGABanner) headView.findViewById(R.id.banner_guide_content);
        bannerGuideContent.setAdapter(new BGABanner.Adapter<ImageView, BannerEntity.DataBean>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, BannerEntity.DataBean bean, int position) {
                Glide.with(getContext())
                        //   .load(Constant.BASE_URL+model.getUrl())
                        .load(Constant.BASE_URL + bean.getUrl())
                        // .placeholder(R.drawable.holder)
                        .error(R.mipmap.a)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });
        bannerGuideContent.setDelegate(iPresenter);

    }

    private void initAdapter() {
        headerAndFooterAdapter = new OneSayAdapter(myWordList, getContext());
        headerAndFooterAdapter.setOnItemClickListener(iPresenter);
        headerAndFooterAdapter.setOnItemChildClickListener(iPresenter);
        headerAndFooterAdapter.openLoadAnimation();
        headerAndFooterAdapter.setOnLoadMoreListener(iPresenter, recyclerView);
        customLoadMoreView = new CustomLoadMoreView();
        headerAndFooterAdapter.setLoadMoreView(customLoadMoreView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(headerAndFooterAdapter);
        recyclerView.setNestedScrollingEnabled(false);
    }


    @Override
    public void goToTop(boolean b) {
        recyclerView.smoothScrollToPosition(0);
    }

    @Override
    protected void lazyLoad() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventListner(BannerEntity bannerEntity) {
        if (bannerEntity instanceof BannerEntity) {
            this.bannerEntity = bannerEntity;
            bindBanner(bannerEntity);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventListner(EventFloatButton eventFloatButton) {
        recyclerView.smoothScrollToPosition(0);
    }

    /*********
     * 语录查询结果，绑定到view
     * @param yiJuAndYiShuoResponse
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventListner(YiJuAndYiShuoResponse yiJuAndYiShuoResponse) {
        if (yiJuAndYiShuoResponse.getCode() == 200 && yiJuAndYiShuoResponse.getMsgType() == 1 && yiJuAndYiShuoResponse.getData().getCount() > 0) {
            YiJuAndYiShuoEntity yiShuoEntity = yiJuAndYiShuoResponse.getData().getItems().get(0);
            Glide.with(getContext()).load(Constant.BASE_URL + yiShuoEntity.getPictureUrl()).into(img);
            textDecs.setText(yiShuoEntity.getContent());
            textPulish.setText(yiShuoEntity.getPublishUser());
            String y;
            int month = TimeUtil.getInt(yiShuoEntity.getUpdatedDate(), Calendar.MONTH);
            if (month < 10) {
                y = "0" + month;
            } else {
                y = month + "";
            }
            textMooth.setText(y);
            textDate.setText(TimeUtil.getInt(yiShuoEntity.getUpdatedDate(), Calendar.DAY_OF_MONTH) + "");
            textNoYulu.setVisibility(View.GONE);
        } else {
            textNoYulu.setVisibility(View.VISIBLE);
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateMusicEvent(MusicUtil.MuiscEvent muiscEvent) {
        switch (muiscEvent.getEventType()) {
            case MusicUtil.MEDIA_STATUS_START:

                break;
            case MusicUtil.MEDIA_STATUS_PREPARE:
            case MusicUtil.MEDIA_STATUS_PLAY:

                break;
            case MusicUtil.MEDIA_STATUS_PAUSE:

                break;
            case MusicUtil.MEDIA_STATUS_STOP:

            case MusicUtil.MEDIA_STATUS_COMLETION:

                break;
        }
    }


    @Override
    public List<YiJuAndYiShuoEntity> getData() {
        return myWordList;
    }

    @Override
    public void startNextAc(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void pullMoreComlite(String... msg) {
        headerAndFooterAdapter.loadMoreComplete();
    }

    @Override
    public void pullMoreNoMore(String... msg) {
        headerAndFooterAdapter.loadMoreComplete();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_END);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void refreshNoData(String... msg) {
        textNoData.setVisibility(View.VISIBLE);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void refreshComplite(String... msg) {
        MusicUtil.getInstance().stop();
        hFrame.refreshComplete();
        headerAndFooterAdapter.notifyDataSetChanged();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_DEFAULT);
        textNoData.setVisibility(View.GONE);
        ShowMsgUtil.hideLoadingDialog();
    }

    @Override
    public void bindBanner(BannerEntity bannerEntity) {
        if (bannerEntity.getData() != null) {
            List<BannerEntity.DataBean> list = new ArrayList<>();
            ArrayList<String> tips = new ArrayList<>();
            for (BannerEntity.DataBean dataBean : bannerEntity.getData()) {
                //  tips.add(dataBean.getTitle());
                tips.add("");
                list.add(dataBean);
                //  list.add("http://opf1eamsp.bkt.clouddn.com/sf.jpg");
            }
            bannerGuideContent.setData(list, tips);
        }
    }

    @Override
    public String getSearchContent() {
        String searchContent = editSearch.getText().toString();
        if (searchContent == null)
            searchContent = "";
        return searchContent;
    }

    @Override
    public int getPageType() {
        return ImpPSay.SHOYE;
    }


    @Override
    public void removeSearchContent() {
        editSearch.setText("");
    }


}
